//
//  NVASettingController.m
//  NVAReader
//
//  Created by DuyLK on 7/1/14.
//  Copyright (c) 2014 NVA Software, Inc. All rights reserved.
//

#import "NVASettingController.h"

@implementation NVASettingController
@synthesize tableViewData;

-(id) init
{
    self = [super init];
    [self setTitle: @"Setting Color Mode"];
    return self;
}

- (void)viewDidLoad
{
	[super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
	[super didReceiveMemoryWarning];
}

- (NSInteger) numberOfSectionsInTableView: (UITableView*)tableView
{
	return 1;
}

- (NSInteger) tableView: (UITableView*)tableView numberOfRowsInSection: (NSInteger)section
{
	return 4;
}

- (UITableViewCell*) tableView: (UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath
{
	static NSString *cellid = @"MuCellIdent";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellid];
	if (!cell)
		cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: cellid] autorelease];
	int row = (int)[indexPath row];
    
    if (row ==0) {
        [[cell textLabel] setText: @"Change color highlight"];
        _buttonHighLight = [[UIView alloc] init];
        UITapGestureRecognizer *singleFingerTap =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(btnHighLight:)];
        [_buttonHighLight addGestureRecognizer:singleFingerTap];
        [singleFingerTap release];
        
        _buttonHighLight.frame = CGRectMake(0, 0, 40, 40);
        _buttonHighLight.layer.cornerRadius = 5;
        [_buttonHighLight.layer setMasksToBounds:YES];
        [_buttonHighLight.layer setBorderColor:[UIColor grayColor].CGColor];
        _buttonHighLight.layer.borderWidth = 2.0f;
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        if([ud boolForKey:@"ColorHighLight"])
        {
            int colorHightLignt = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"ColorHighLight"];
             UIColor *colorhight = [self colorDecode:colorHightLignt];
            _buttonHighLight.backgroundColor = colorhight;
        }
        else
        {
            _buttonHighLight.backgroundColor = [UIColor yellowColor];
        }
        [cell setAccessoryView: _buttonHighLight];
    }else if(row == 1)
    {
        [[cell textLabel] setText: @"Change color underline"];
        _buttonUnderLine = [[UIView alloc] init];
        UITapGestureRecognizer *singleFingerTap =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(btnUnder:)];
        [_buttonUnderLine addGestureRecognizer:singleFingerTap];
        [singleFingerTap release];
        _buttonUnderLine.frame = CGRectMake(0, 0, 40, 40);
        _buttonUnderLine.layer.cornerRadius = 5;
        [_buttonUnderLine.layer setMasksToBounds:YES];
        [_buttonUnderLine.layer setBorderColor:[UIColor grayColor].CGColor];
        _buttonUnderLine.layer.borderWidth = 2.0f;
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        if([ud boolForKey:@"ColorUnderLine"])
        {
            int colorHightLignt = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"ColorUnderLine"];
            UIColor *colorhight = [self colorDecode:colorHightLignt];
            _buttonUnderLine.backgroundColor = colorhight;
        }
        else
        {
            _buttonUnderLine.backgroundColor = [UIColor blueColor];
        }
        [cell setAccessoryView: _buttonUnderLine];
    }else if(row == 2)
    {
        [[cell textLabel] setText: @"Change color strikeout"];
        _buttonStrikeOut = [[UIView alloc] init];
        UITapGestureRecognizer *singleFingerTap =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(btnStrike:)];
        [_buttonStrikeOut addGestureRecognizer:singleFingerTap];
        [singleFingerTap release];
        _buttonStrikeOut.frame = CGRectMake(0, 0, 40, 40);
        _buttonStrikeOut.layer.cornerRadius = 5;
        [_buttonStrikeOut.layer setMasksToBounds:YES];
        [_buttonStrikeOut.layer setBorderColor:[UIColor grayColor].CGColor];
        _buttonStrikeOut.layer.borderWidth = 2.0f;
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        if([ud boolForKey:@"ColorStrikeOut"])
        {
            int colorHightLignt = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"ColorStrikeOut"];
            UIColor *colorhight = [self colorDecode:colorHightLignt];
            _buttonStrikeOut.backgroundColor = colorhight;
        }
        else
        {
            _buttonStrikeOut.backgroundColor = [UIColor redColor];
        }
        [cell setAccessoryView: _buttonStrikeOut];
    }
    else if(row == 3)
    {
        [[cell textLabel] setText: @"Change color draw by hand"];
        _buttonDraw = [[UIView alloc ]init];
        
        UITapGestureRecognizer *singleFingerTap =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(btnDraw:)];
        [_buttonDraw addGestureRecognizer:singleFingerTap];
        
        _buttonDraw.frame = CGRectMake(0, 0, 40, 40);
        _buttonDraw.layer.cornerRadius = 5;
        [_buttonDraw.layer setMasksToBounds:YES];
        [_buttonDraw.layer setBorderColor:[UIColor grayColor].CGColor];
        _buttonDraw.layer.borderWidth = 2.0f;
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        if([ud boolForKey:@"ColorDraw"])
        {
            int colorHightLignt = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"ColorDraw"];
            UIColor *colorhight = [self colorDecode:colorHightLignt];
            _buttonDraw.backgroundColor = colorhight;
        }
        else
        {
            _buttonDraw.backgroundColor = [UIColor redColor];
        }
        [cell setAccessoryView: _buttonDraw];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"Choose color tool";
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
     return @"Set your color for annotation mode";
}

-(IBAction)btnHighLight:(UITapGestureRecognizer *)recognizer
{
    index = 0;
    [_colorPickerView setHidden:NO];
    [_buttonOK setHidden:NO];
    [_buttonCancel setHidden:NO];
    [tableViewData setHidden:YES];
    _colorPickerView.color = _buttonHighLight.backgroundColor;
    [self.view setNeedsDisplay];
}

-(IBAction)btnUnder:(UITapGestureRecognizer *)recognizer
{
    index = 1;
    [_colorPickerView setHidden:NO];
    [_buttonOK setHidden:NO];
    [_buttonCancel setHidden:NO];
    [tableViewData setHidden:YES];
    [self.view setNeedsDisplay];
}

-(IBAction)btnStrike:(UITapGestureRecognizer *)recognizer
{
    index = 2;
    [_colorPickerView setHidden:NO];
    [_buttonOK setHidden:NO];
    [_buttonCancel setHidden:NO];
    [tableViewData setHidden:YES];
    [self.view setNeedsDisplay];
}


-(IBAction)btnDraw:(UITapGestureRecognizer *)recognizer
{
    index = 3;
    [_colorPickerView setHidden:NO];
    [_buttonOK setHidden:NO];
    [_buttonCancel setHidden:NO];
    [tableViewData setHidden:YES];
    [self.view setNeedsDisplay];
}

NKOColorPickerDidChangeColorBlock colorDidChangeBlock = ^(UIColor *color){
    
};


-(void) loadView
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    
    UIView *view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
	[view setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
	[view setAutoresizesSubviews: YES];
    [view setBackgroundColor: [UIColor whiteColor]];
    [self setView: view];
	[view release];
    
    NSString *ver = [[UIDevice currentDevice] systemVersion];
    int ver_int = [ver intValue];
    
    if (ver_int < 7) {
        tableViewData = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 320, 480) style:UITableViewStyleGrouped];
    }else
    {
        tableViewData = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStyleGrouped];
    }
    [tableViewData setAutoresizesSubviews:YES];
    [tableViewData setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
    tableViewData.rowHeight = 45;
    tableViewData.sectionFooterHeight = 22;
    tableViewData.sectionHeaderHeight = 22;
    tableViewData.scrollEnabled = YES;
    tableViewData.backgroundColor = [UIColor whiteColor];
    tableViewData.showsVerticalScrollIndicator = YES;
    tableViewData.userInteractionEnabled = YES;
    tableViewData.bounces = YES;
   // tableViewData
    [tableViewData setDataSource:self];
    [tableViewData setDelegate:self];
    [[self view] addSubview:tableViewData];
    
    _colorPickerView = [[NVAColorPickerView alloc] initWithFrame:CGRectMake(0, 60, 320,320) color:[UIColor blueColor] andDidChangeColorBlock:colorDidChangeBlock];
    [_colorPickerView setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    [_colorPickerView setAutoresizesSubviews: YES];
    
    //Add color picker to your view
    [self.view addSubview:_colorPickerView];
    [_colorPickerView setHidden:YES];
    
    _buttonOK = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_buttonOK addTarget:self
                  action:@selector(btnOK:)
        forControlEvents:UIControlEventTouchUpInside];
    [_buttonOK setTitle:@"OK" forState:UIControlStateNormal];
    _buttonCancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_buttonCancel addTarget:self
                      action:@selector(bntCancel:)
            forControlEvents:UIControlEventTouchUpInside];
    [_buttonCancel setTitle:@"Cancel" forState:UIControlStateNormal];
    [self.view addSubview:_buttonCancel];
    [self.view addSubview:_buttonOK];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
	{
        _buttonCancel.frame = CGRectMake(screenWidth/2 + 10, screenWidth + 150, 80, 40);
        _buttonOK.frame = CGRectMake(screenWidth/2 - 90,screenWidth + 150, 80, 40);
    }else{
        _buttonCancel.frame = CGRectMake(screenWidth/2 + 10, _colorPickerView.frame.size.height + 150, 80, 40);
        _buttonOK.frame = CGRectMake(screenWidth/2 - 90, _colorPickerView.frame.size.height + 150, 80, 40);
    }
    [_buttonOK setHidden:YES];
    [_buttonCancel setHidden:YES];
}

-(IBAction)btnOK:(id)sender
{
    [_buttonOK setHidden:YES];
    [_buttonCancel setHidden:YES];
     [_colorPickerView setHidden:YES];
    [tableViewData setHidden:NO];
    if (index == 0) {
        UIColor *color = _colorPickerView.color;
        int colorDraw = [self colorCode:color];
        _buttonHighLight.backgroundColor = color;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setInteger:colorDraw forKey: @"ColorHighLight"];
        [defaults synchronize];
        [_buttonHighLight setNeedsDisplay];
    }else if(index  ==1){
        UIColor *color = _colorPickerView.color;
        int colorDraw = [self colorCode:color];
        _buttonUnderLine.backgroundColor = color;
          NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
         [defaults setInteger:colorDraw forKey: @"ColorUnderLine"];
        [defaults synchronize];
        [_buttonUnderLine setNeedsDisplay];
    }else if(index  ==2){
        UIColor *color = _colorPickerView.color;
        int colorDraw = [self colorCode:color];
        _buttonStrikeOut .backgroundColor = color;
          NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
         [defaults setInteger:colorDraw forKey: @"ColorStrikeOut"];
        [defaults synchronize];
        [_buttonStrikeOut setNeedsDisplay];
    }else if(index  ==3){
        UIColor *color = _colorPickerView.color;
        int colorDraw = [self colorCode:color];
          NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
         [defaults setInteger:colorDraw forKey: @"ColorDraw"];
        [defaults synchronize];
        _buttonDraw.backgroundColor =color;
        [_buttonDraw setNeedsDisplay];
    }
}

-(IBAction)bntCancel:(id)sender
{
    [_buttonOK setHidden:YES];
    [_buttonCancel setHidden:YES];
    [_colorPickerView setHidden:YES];
    [tableViewData setHidden:NO];
}

- (int)colorCode:(UIColor *)color
{
    CGFloat red, green, blue, alphat;
    if ([color getRed:&red green:&green blue:&blue alpha:&alphat])
    {
        int redInt = (int)(red * 255 + 0.5);
        int greenInt = (int)(green * 255 + 0.5);
        int blueInt = (int)(blue * 255 + 0.5);
        int alphaInt = (int)(alphat * 255 + 0.5);
        return (alphaInt <<24) | (redInt << 16) | (greenInt << 8) | blueInt;
    }
    return 0;
}

-(UIColor*)colorDecode:(int) color;
{
    float alphaInt = (float)(color>>24 & 0x000000FF)/255.0;
    float redInt = (color>>16 & 0x000000FF)/255.0;
    float greenInt = (color>>8 & 0x000000FF)/255.0;
    float blueInt = (color & 0x000000FF)/255.0;
    UIColor *colortemp = [[UIColor alloc] initWithRed:redInt green:greenInt blue:blueInt alpha:alphaInt];
    return colortemp;
}
@end
